package com.hp.attendance.serlvet.employee;

import com.hp.attendance.entity.Role;
import com.hp.attendance.entity.dto.EmpRoleDto;
import com.hp.attendance.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/distriEmp")
public class EmpDistriSerlvet extends HttpServlet {
    private RoleService roleService = new RoleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //分配角色 回显
        try {
            String emp_id = req.getParameter("emp_id");
            //根据员工id 查询出所有的角色的列表
            List<Role> roles = roleService.getRoleByEmpId(Integer.parseInt(emp_id));
            //查询出所有的角色列表
            List<Role> list = roleService.getList();
            ArrayList<EmpRoleDto> listerd = new ArrayList<EmpRoleDto>();
            for (Role role : list) {
                EmpRoleDto erd = new EmpRoleDto();
                erd.setRole(role);
                erd.setEmpId(Integer.parseInt(emp_id));
                for (Role r : roles) {
                    if(role.getId()==r.getId()){
                        erd.setCheck(true);
                    }
                }
                listerd.add(erd);
            }
            req.setAttribute("empId",emp_id);
            req.setAttribute("listerd",listerd);
            req.getRequestDispatcher("/pages/user_role_relation.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
